package zhoug.zui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 图形:圆,矩形,框,线
 *
 * @Author 35574
 * @Date 2020/9/14
 * @Description
 */
public class ZUIShape extends View {
    private static final String TAG = ">>>MarkerLabelIcon";
    private static final boolean DEBUG = false;

    /**
     * 圆:●
     */
    public static final int CIRCLE = 1;
    /**
     * 环:
     */
    public static final int RING = 2;
    /**
     * 矩形:■
     */
    public static final int RECT = 3;
    /**
     * 方框框 : □
     * */
    public static final int FRAME = 4;
    /**
     * 线:——
     * 默认宽度为高度的1/3
     */
    public static final int LINE = 5;
    /**
     * 线圈,线中间有个空心圆 :——0——
     */
    public static final int LINE_CIRCLE = 6;

    /**
     * 图形形状
     * {@link #CIRCLE}
     * {@link #RING}
     * {@link #RECT}
     * {@link #FRAME}
     * {@link #LINE}
     * {@link #LINE_CIRCLE}
     */
    private int shape = CIRCLE;

    /**
     * 颜色
     */
    private int shapeColor;
    /**
     * 宽度:环形宽度,方框宽度,线的宽度
     */
    private float shapeWidth;


    private Paint mPaint;

    public ZUIShape(Context context) {
        this(context, null);

    }

    public ZUIShape(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZUIShape(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZUIShape, defStyleAttr, 0);
        shape = a.getInt(R.styleable.ZUIShape_zui_shape_shape, RECT);
        shapeColor = a.getColor(R.styleable.ZUIShape_zui_shape_color, Color.BLUE);
        shapeWidth = a.getDimension(R.styleable.ZUIShape_zui_shape_width, 0);
        a.recycle();

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setColor(shapeColor);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (shape == CIRCLE) {
            //画圆
            int width = getWidth();
            int height = getHeight();
            float r = width > height ? height >> 1 : width >> 1;
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(0);
            mPaint.setColor(shapeColor);
            canvas.drawCircle(width >> 1, height >> 1, r, mPaint);
        } else if (shape == RING) {
            //画环
            int width = getWidth();
            int height = getHeight();
            float r = width > height ? height >> 1 : width >> 1;
            if (shapeWidth <= 0) {
                shapeWidth = 0.2f * r;
            }else if(shapeWidth>r){
                shapeWidth=r;
            }
            r -= shapeWidth / 2;
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(shapeWidth);
            mPaint.setColor(shapeColor);
            canvas.drawCircle(width >> 1, height >> 1, r, mPaint);

        } else if (shape == LINE) {
            //画线
            int height = getHeight();
            if (shapeWidth <= 0) {
                shapeWidth = height / 3.0f;
            }else if(shapeWidth>height){
                shapeWidth=height;
            }
            float top = (height - shapeWidth) / 2.0f;
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(0);
            mPaint.setColor(shapeColor);
            canvas.drawRect(0, top, getWidth(), top + shapeWidth, mPaint);
        } else if (shape==FRAME) {
            //画框
            if (shapeWidth <= 0) {
                shapeWidth = Math.min(getWidth(),getHeight())/5.0f;
            }
            float ban = shapeWidth / 2;
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(shapeWidth);
            mPaint.setColor(shapeColor);
            canvas.drawRect(ban,ban,getWidth()-ban,getHeight()-ban, mPaint);
        }else if(shape==LINE_CIRCLE){
            int width = getWidth();
            float shapeWidthBan = shapeWidth / 2;
            float radius=width/6.0f-shapeWidthBan;
            float cx=width>>1;
            float cy=getHeight()>>1;

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(shapeWidth);
            mPaint.setColor(shapeColor);
            canvas.drawCircle(cx,cy,radius,mPaint);
            canvas.drawLine(0,cy,width/3.0f+shapeWidthBan,cy,mPaint);
            canvas.drawLine(width*2/3.0f-shapeWidthBan,cy,width,cy,mPaint);


        } else {
            //画矩形
            canvas.drawColor(shapeColor);
        }
        super.onDraw(canvas);
    }


    public void setShape(int shape) {
        this.shape = shape;
    }

    public void setShapeColor(int shapeColor) {
        this.shapeColor = shapeColor;
    }

    public void setShapeWidth(float shapeWidth) {
        this.shapeWidth = shapeWidth;
    }

}
